<template>
  <div class="question-container">
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="学科名称">
              <el-input v-model="requestParams.subjectName" style="200px" />
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clear()">清除</el-button>
              <el-button type="primary" @click="filter()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align:right">
          <el-button icon="el-icon-edit" type="success" size="small" @click="openDialog()">新增学科</el-button>
        </el-col>
      </el-row>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom:15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      />
      <!-- 表格 -->
      <el-table :data="subjects">
        <el-table-column label="序号" type="index" :index="indexMethod" />
        <el-table-column label="学科名称" prop="subjectName" />
        <el-table-column label="创建者" prop="username" />
        <el-table-column label="创建日期" width="160px">
          <template #default="{row}">{{ row.addDate|parseTimeByString }}</template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template #default="{row}">{{ row.isFrontDisplay===1?'是':'否' }}</template>
        </el-table-column>
        <el-table-column label="二级目录" prop="twoLevelDirectory" />
        <el-table-column label="标签" prop="tags" />
        <el-table-column label="题目数量" prop="totals" />
        <el-table-column label="操作" width="240px">
          <template #default="{row}">
            <el-button type="text" @click="$router.push(`directorys?id=${row.id}&name=${row.subjectName}`)">学科分类</el-button>
            <el-button type="text" @click="$router.push(`tags?id=${row.id}&name=${row.subjectName}`)">学科标签</el-button>
            <el-button type="text" @click="openDialog(row)">修改</el-button>
            <el-button type="text" @click="removeSubjects(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px;text-align:right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        :page-sizes="[5,10,20,50]"
        @current-change=" handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </el-card>
    <!-- 修改编辑 -->
    <subjects-add
      ref="subjectsadd"
      :data="currSubject"
      @updateList="updateList()"
    />
  </div>
</template>
<script>
import { list as getSubjects, remove } from '@/api/hmmm/subjects'
import SubjectsAdd from '../components/subjects-add'
export default {
  name: 'SubjectsList',
  components: { SubjectsAdd },
  data() {
    return {
      requestParams: {
        subjectName: '',
        page: 1,
        pagesize: 10
      },
      subjects: [],
      total: 0,
      currSubject: {}
    }
  },

  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const { data } = await getSubjects(this.requestParams)
      this.subjects = data.items
      this.total = data.counts
    },
    removeSubjects(id) {
      this.$confirm('此操作将永久删除该学科，是否继续', '提示', {
        type: 'warning'
      })
        .then(async() => {
          const res = await remove(id)
          console.log(res)
          this.$message.success('删除学科成功')
          this.getList()
        })
        .catch(() => {
          console.log('取消')
        })
    },
    handleCurrentChange(value) {
      this.requestParams.page = value
      this.getList()
    },
    handleSizeChange(value) {
      this.requestParams.pagesize = value
      this.requestParams.page = 1
      this.getList()
    },
    openDialog(subject) {
      this.currSubject = subject || {}
      this.$nextTick(() => {
        this.$refs.subjectsadd.open()
      })
    },
    updateList() {
      if (!this.currSubject.id) {
        this.requestParams.page = 1
      }
      this.getList()
    },
    filter() {
      this.requestParams.page = 1
      this.getList()
    },
    clear() {
      this.requestParams.subjectName = ''
      this.getList()
    },
    indexMethod(index) {
      // index 行号 从0开始   0 1 2 3
      // return index + 1 + 前面一共有多少条数据
      return index + 1 + this.requestParams.pagesize * (this.requestParams.page - 1)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

</style>

<style>

</style>

<style>
</style>

